#include "../include/Reactor/TcpServer.h"
#include "../include/Reactor/EchoServer.h"
#include "../include/Configuration.hh"
#include "../include/Dictionary.hh"
#include "../include/Cache/CacheManager.hh"
#include "../include/Log/Log.hh"
#include "../include/Cache/TimerManager.hh"
#include <string>
#include <iostream>
#include <signal.h>


#define THREAD_NUM 4
#define QUEUE_SIZE 1024


using std::string;

void sig_handler(int signum){

    Log::error("收到了信号%d", signum);

    auto& manager = CacheManager::createCacheManager()->getManager();
    // 缓存写入文件
    // 只写一个缓存就好了, 用于同步的那个缓存
    auto& conf_map = Configuration::createConfig()->getConfigMap();
    manager.writeToFile(conf_map["cacahe_file"][0]);
    
    Log::info("缓存写入文件成功");
    Log::info("进程退出");
    exit(0);
}



int main(){

    ::signal(SIGINT, sig_handler);
    // Log::createLog(CONSOLE | FILE | ROLL, Priority::DEBUG);
    Log::createLog(CONSOLE | ROLL, Priority::INFO);
    Log::info("日志启动");

    TimerManager::createTimer(1, 10);
    CacheManager::createCacheManager();
    
    auto& conf_map = Configuration::createConfig()->getConfigMap();
    Log::info("配置文件加载成功");

    Dictionary::createInstance();
    Log::info("词典加载成功");

    EchoServer server(
        THREAD_NUM,
        QUEUE_SIZE,
        conf_map["ip"][0], 
        atoi(conf_map["port"][0].c_str())
    );

    server.start();

    // server.stop();

    return 0;
}